$(function () {
    var $datagrid = $("#datagrid"), $toolbar = $("#toolbar"), $sex=$("#sex");

    $sex.combobox({
        onChange:function(v,o){
            doSearch();
        }
    });

    $datagrid.datagrid({
        singleSelect: true,
        fit: true,
        border: false,
        fitColumns: true,
        url: Constant.ctx("/mulanweb/admin/user/search"),
        method: 'post',
        pagination: true,
        toolbar: $toolbar,
        columns: [[
            {field: 'Id', title: '编号'},
            {field: 'OpenId', title: '微信编号'},
            {field: 'Mobile_Number', title: '手机号码',formatter:function(v,r,i){
                if(v){
                    return v ;
                }else{
                    return "未填写";
                }
            }},
            {field: 'WechatNickname', title: '昵称'},
            {field: 'Sex', title: '性别',formatter:function(v,r,i){
                if(v == 1){
                    return "男";
                }else if(v== 2){
                    return "女";
                }else{
                    return "未填写";
                }
            }},
            {field:'CreateTime',title:'注册时间'}
        ]],
       onBeforeLoad: function (param) {
            param.PageNumber = param.page;
            param.PageSize = param.rows;
            var sex = $("#sex").combo("getValue");
           if(sex && sex != 0 ){
               param.sex = sex;
           }
            var mobile = $("#mobile").val();
           if(mobile){
               param.mobile_number= mobile;
           }
           var nickname = $("#nickname").val();
           if(nickname){
               param.wechat_nickname = nickname;
           }
            return true;
        },
        loadFilter: function (data) {
            var newData = {};
            if (data.ReturnCode == 1) {
                newData.rows = data.DataList;
                if (newData.rows.length > 0) {
                    newData.total = data.TotalRow;
                } else {
                    newData.total = 0;
                }
            }
            return newData;
        },
        onDblClickRow:function (index, rows) {
            var para = {
                userId : rows.Id,
                PageSize:10,
                PageNumber:1
            }
            $.post(Constant.ctx("/mulanweb/admin/chat_history/searchUser"),para,function(info){
                for(var i in info.DataList){
                    var dataTime = new Date(info.DataList[i].Sent_time*1000);
                    info.DataList[i].Sent_time =dataTime.toLocaleDateString() + " " + dataTime.toLocaleTimeString() ;

                    var chatType = info.DataList[i].ChatType;
                    if(chatType == "2002"){
                        info.DataList[i].TagType = false;
                    }else{
                        info.DataList[i].TagType = true;
                    }
                }
                var module = $("#detail").html();
                var compileModule = Template7.compile(module);
                var resultModule = compileModule(info);
                $("#dialogDetail").html(resultModule);
            })

            $("#buttons").pagination({

            });

            $("#dialogDetail").dialog({
                title: '详情',
                width: 500,
                height: 500,
                buttons:"#buttons"
            });
        }
    });
})
function doSearch(){
    $("#datagrid").datagrid("reload");
}